<!--
Copyright (c) 2019-2026, Hossein Moein
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.
* Neither the name of Hossein Moein and/or the DataFrame nor the
  names of its contributors may be used to endorse or promote products
  derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Hossein Moein BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!DOCTYPE html>
<html>

<head>
<style>
body {
  background-image: linear-gradient(Azure, AliceBlue, GhostWhite, WhiteSmoke);
}
</style>
</head>

<body style="font-family: Georgia, serif">
  <font size="+3">&#8592;</font> <a href="https://htmlpreview.github.io/?https://github.com/hosseinmoein/DataFrame/blob/master/docs/HTML/DataFrame.html">Back to Documentations</a><BR><BR>
  
  <table border="1">

    <tr bgcolor="lightblue">
      <th>Signature</th> <th>Description</th> <th>Parameters</th>
    </tr>
    <tr bgcolor="Azure">
      <td>
<pre class="code_syntax" style="color:#000000;background:#ffffff00;"><span class="line_wrapper"><span style="color:#004a43; ">#</span><span style="color:#004a43; ">include </span><span style="color:#800000; ">&lt;</span><span style="color:#40015a; ">DataFrame/DataFrameMLVisitors.h</span><span style="color:#800000; ">&gt;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper"><span style="color:#800000; font-weight:bold; ">template</span><span style="color:#800080; ">&lt;</span><span style="color:#603000; ">size_t</span> K<span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">typename</span> T<span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">typename</span> I <span style="color:#808030; ">=</span> <span style="color:#800000; font-weight:bold; ">unsigned</span> <span style="color:#800000; font-weight:bold; ">long</span><span style="color:#808030; ">,</span></span>
<span class="line_wrapper">         <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span><span style="color:#603000; ">size_t</span> A <span style="color:#808030; ">=</span> <span style="color:#008c00; ">0</span><span style="color:#800080; ">&gt;</span></span>
<span class="line_wrapper"><span style="color:#800000; font-weight:bold; ">struct</span> KMeansVisitor<span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span></pre>
      </td>
      <td>
        This is a single action visitor, meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface.<BR><BR>
        This functor class finds the K means in the data. It could also cluster the data around the means.<BR>
        The constructor takes 3 parameters<BR>
        <OL>
        <LI>Number of iterations</LI>
        <LI>If <I>calc_clusters</I> is true -- default -- the actual clusters are also calculated in addition to the means</LI>
        <LI>A function to calculate distance between two data points of type T (with default)</LI>
        </OL>
        <I>
        <PRE>
KMeansVisitor(std::size_t num_of_iter,
              bool calc_clusters = true,
              distance_func f = [](const T &x, const T &y) -> double {
                                    return ((x - y) * (x - y));
                                })
        </PRE>
        </I>
        </LI>
        </OL>
        <I>get_results()</I> returns an array of K means.<BR><BR>
        <I>get_clusters()</I> returns an array of K <I>VectorPtrView</I>'s which contain the data clustered around the K-Means. The first element in each <I>VectorPtrView</I> is the mean and the reset are the data points belonging to that cluster.<BR><BR>
        <I>get_clusters_idxs()</I> returns an array of K <I>std::vector&lt;size_type&gt;</I>'s which contains indices of the data in each cluster.<BR>
       </td>
      <td width="30%">
        <B>K</B>: Number of means to find<BR>
        <B>T</B>: Column data type<BR>
        <B>I</B>: Index type<BR>
        <B>A</B>: Memory alignment boundary for vectors. Default is system default alignment<BR>
      </td>
    </tr>

  </table>

<pre class="code_syntax" style="color:#000000;background:#ffffff00;"><span class="line_wrapper"><span style="color:#800000; font-weight:bold; ">struct</span>  Point  <span style="color:#800080; ">{</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">double</span>  x <span style="color:#800080; ">{</span> <span style="color:#008000; ">0.0</span> <span style="color:#800080; ">}</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">double</span>  y <span style="color:#800080; ">{</span> <span style="color:#008000; ">0.0</span> <span style="color:#800080; ">}</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    Point<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#808030; ">=</span> <span style="color:#800000; font-weight:bold; ">default</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    Point<span style="color:#808030; ">(</span><span style="color:#800000; font-weight:bold; ">double</span> xx<span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">double</span> yy<span style="color:#808030; ">)</span> <span style="color:#800080; ">:</span> x<span style="color:#808030; ">(</span>xx<span style="color:#808030; ">)</span><span style="color:#808030; ">,</span> y<span style="color:#808030; ">(</span>yy<span style="color:#808030; ">)</span>  <span style="color:#800080; ">{</span>   <span style="color:#800080; ">}</span></span>
<span class="line_wrapper">    Point<span style="color:#808030; ">(</span><span style="color:#800000; font-weight:bold; ">const</span> Point <span style="color:#808030; ">&amp;</span><span style="color:#808030; ">)</span> <span style="color:#808030; ">=</span> <span style="color:#800000; font-weight:bold; ">default</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    Point <span style="color:#808030; ">&amp;</span><span style="color:#800000; font-weight:bold; ">operator</span> <span style="color:#808030; ">=</span> <span style="color:#808030; ">(</span><span style="color:#800000; font-weight:bold; ">const</span> Point <span style="color:#808030; ">&amp;</span><span style="color:#808030; ">)</span> <span style="color:#808030; ">=</span> <span style="color:#800000; font-weight:bold; ">default</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">friend</span> Point <span style="color:#800000; font-weight:bold; ">operator</span> <span style="color:#808030; ">+</span> <span style="color:#808030; ">(</span><span style="color:#800000; font-weight:bold; ">const</span> Point <span style="color:#808030; ">&amp;</span>lhs<span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">const</span> Point <span style="color:#808030; ">&amp;</span>rhs<span style="color:#808030; ">)</span>  <span style="color:#800080; ">{</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">        <span style="color:#800000; font-weight:bold; ">return</span> <span style="color:#808030; ">(</span>Point<span style="color:#808030; ">(</span>lhs<span style="color:#808030; ">.</span>x <span style="color:#808030; ">+</span> rhs<span style="color:#808030; ">.</span>x<span style="color:#808030; ">,</span> lhs<span style="color:#808030; ">.</span>y <span style="color:#808030; ">+</span> rhs<span style="color:#808030; ">.</span>y<span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    <span style="color:#800080; ">}</span></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">friend</span> Point <span style="color:#800000; font-weight:bold; ">operator</span> <span style="color:#808030; ">/</span> <span style="color:#808030; ">(</span><span style="color:#800000; font-weight:bold; ">const</span> Point <span style="color:#808030; ">&amp;</span>lhs<span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">double</span> rhs<span style="color:#808030; ">)</span>  <span style="color:#800080; ">{</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">        <span style="color:#800000; font-weight:bold; ">return</span> <span style="color:#808030; ">(</span>Point<span style="color:#808030; ">(</span>lhs<span style="color:#808030; ">.</span>x <span style="color:#808030; ">/</span> rhs<span style="color:#808030; ">,</span> lhs<span style="color:#808030; ">.</span>y <span style="color:#808030; ">/</span> rhs<span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    <span style="color:#800080; ">}</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">template</span><span style="color:#800080; ">&lt;</span><span style="color:#800000; font-weight:bold; ">typename</span> S<span style="color:#800080; ">&gt;</span></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">friend</span> S <span style="color:#808030; ">&amp;</span><span style="color:#800000; font-weight:bold; ">operator</span> <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> <span style="color:#808030; ">(</span>S <span style="color:#808030; ">&amp;</span>s<span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">const</span> Point <span style="color:#808030; ">&amp;</span>rhs<span style="color:#808030; ">)</span>  <span style="color:#800080; ">{</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">        <span style="color:#800000; font-weight:bold; ">return</span> <span style="color:#808030; ">(</span>s <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> rhs<span style="color:#808030; ">.</span>x <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> <span style="color:#800000; ">"</span><span style="color:#0000e6; ">, </span><span style="color:#800000; ">"</span> <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> rhs<span style="color:#808030; ">.</span>y<span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    <span style="color:#800080; ">}</span></span>
<span class="line_wrapper"><span style="color:#800080; ">}</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper"><span style="color:#800000; font-weight:bold; ">static</span> <span style="color:#800000; font-weight:bold; ">double</span> point_distance<span style="color:#808030; ">(</span><span style="color:#800000; font-weight:bold; ">const</span> Point <span style="color:#808030; ">&amp;</span>lhs<span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">const</span> Point <span style="color:#808030; ">&amp;</span>rhs<span style="color:#808030; ">)</span>  <span style="color:#800080; ">{</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">return</span> <span style="color:#808030; ">(</span><span style="color:#808030; ">(</span>lhs<span style="color:#808030; ">.</span>x <span style="color:#808030; ">-</span> rhs<span style="color:#808030; ">.</span>x<span style="color:#808030; ">)</span> <span style="color:#808030; ">*</span> <span style="color:#808030; ">(</span>lhs<span style="color:#808030; ">.</span>x <span style="color:#808030; ">-</span> rhs<span style="color:#808030; ">.</span>x<span style="color:#808030; ">)</span> <span style="color:#808030; ">+</span> <span style="color:#808030; ">(</span>lhs<span style="color:#808030; ">.</span>y <span style="color:#808030; ">-</span> rhs<span style="color:#808030; ">.</span>y<span style="color:#808030; ">)</span> <span style="color:#808030; ">*</span> <span style="color:#808030; ">(</span>lhs<span style="color:#808030; ">.</span>y <span style="color:#808030; ">-</span> rhs<span style="color:#808030; ">.</span>y<span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"><span style="color:#800080; ">}</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper"><span style="color:#800000; font-weight:bold; ">static</span> <span style="color:#800000; font-weight:bold; ">void</span> test_k_means<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>  <span style="color:#800080; ">{</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span><span style="color:#603000; ">cout</span> <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> <span style="color:#800000; ">"</span><span style="color:#0f69ff; ">\n</span><span style="color:#0000e6; ">Testing k-means visitor ...</span><span style="color:#800000; ">"</span> <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span><span style="color:#603000; ">endl</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">const</span> <span style="color:#603000; ">size_t</span>            item_cnt <span style="color:#808030; ">=</span> <span style="color:#008c00; ">1024</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    MyDataFrame             df<span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    RandGenParams<span style="color:#800080; ">&lt;</span><span style="color:#800000; font-weight:bold; ">double</span><span style="color:#800080; ">&gt;</span>   p<span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    p<span style="color:#808030; ">.</span>mean <span style="color:#808030; ">=</span> <span style="color:#008000; ">1.0</span><span style="color:#800080; ">;</span>  <span style="color:#696969; ">// Default</span></span>
<span class="line_wrapper">    p<span style="color:#808030; ">.</span><span style="color:#666616; ">std</span> <span style="color:#808030; ">=</span> <span style="color:#008000; ">0.005</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    p<span style="color:#808030; ">.</span>seed <span style="color:#808030; ">=</span> <span style="color:#008c00; ">10</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    df<span style="color:#808030; ">.</span>load_data<span style="color:#808030; ">(</span>MyDataFrame<span style="color:#800080; ">::</span>gen_sequence_index<span style="color:#808030; ">(</span><span style="color:#008c00; ">0</span><span style="color:#808030; ">,</span> item_cnt<span style="color:#808030; ">,</span> <span style="color:#008c00; ">1</span><span style="color:#808030; ">)</span><span style="color:#808030; ">,</span></span>
<span class="line_wrapper">                 <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span>make_pair<span style="color:#808030; ">(</span><span style="color:#800000; ">"</span><span style="color:#0000e6; ">col1</span><span style="color:#800000; ">"</span><span style="color:#808030; ">,</span> gen_lognormal_dist<span style="color:#800080; ">&lt;</span><span style="color:#800000; font-weight:bold; ">double</span><span style="color:#808030; ">,</span> <span style="color:#008c00; ">128</span><span style="color:#800080; ">&gt;</span><span style="color:#808030; ">(</span>item_cnt<span style="color:#808030; ">,</span> p<span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    KMeansVisitor<span style="color:#800080; ">&lt;</span><span style="color:#008c00; ">5</span><span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">double</span><span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">unsigned</span> <span style="color:#800000; font-weight:bold; ">long</span><span style="color:#808030; ">,</span> <span style="color:#008c00; ">128</span><span style="color:#800080; ">&gt;</span>    km_visitor<span style="color:#808030; ">(</span><span style="color:#008c00; ">1000</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    df<span style="color:#808030; ">.</span>single_act_visit<span style="color:#800080; ">&lt;</span><span style="color:#800000; font-weight:bold; ">double</span><span style="color:#800080; ">&gt;</span><span style="color:#808030; ">(</span><span style="color:#800000; ">"</span><span style="color:#0000e6; ">col1</span><span style="color:#800000; ">"</span><span style="color:#808030; ">,</span> km_visitor<span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span><span style="color:#603000; ">cout</span> <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> <span style="color:#800000; ">"</span><span style="color:#0000e6; ">Means of clusters are: </span><span style="color:#800000; ">"</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">for</span> <span style="color:#808030; ">(</span><span style="color:#800000; font-weight:bold; ">const</span> <span style="color:#800000; font-weight:bold; ">auto</span> citer <span style="color:#800080; ">:</span> km_visitor<span style="color:#808030; ">.</span>get_result<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span><span style="color:#808030; ">)</span></span>
<span class="line_wrapper">        <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span><span style="color:#603000; ">cout</span> <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> citer <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> <span style="color:#800000; ">"</span><span style="color:#0000e6; ">, </span><span style="color:#800000; ">"</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span><span style="color:#603000; ">cout</span> <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span><span style="color:#603000; ">endl</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper"></span>
<span class="line_wrapper"><span style="color:#696969; ">/*</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    // Using the calculated means, separate the given column into clusters</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    const auto  &amp;clusters = km_visitor.get_clusters();</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    bool        found = false;</span></span>
<span class="line_wrapper"><span style="color:#696969; "></span></span>
<span class="line_wrapper"><span style="color:#696969; ">    for (auto iter : clusters)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        if (::fabs(iter[0] - 1.89348) &lt; 0.00001)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            if (::fabs(iter[6] - 1.44231) &lt; 0.00001)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                found = true;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                break;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    assert(found);</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    found = false;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    for (auto iter : clusters)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        if (::fabs(iter[0] - 0.593126) &lt; 0.00001)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            if (::fabs(iter[2] - 0.950026) &lt; 0.00001)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                found = true;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                break;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    assert(found);</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    found = false;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    for (auto iter : clusters)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        if (::fabs(iter[0] - 14.2245) &lt; 0.0001)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            found = true;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            break;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    assert(found);</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    found = false;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    for (auto iter : clusters)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        if (::fabs(iter[0] - 6.90427) &lt; 0.00001)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            found = true;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            break;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    assert(found);</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    found = false;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    for (auto iter : clusters)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        if (::fabs(iter[0] - 3.8146) &lt; 0.00001)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            found = true;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            break;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    assert(found);</span></span>
<span class="line_wrapper"><span style="color:#696969; ">*/</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    <span style="color:#696969; ">// Now try with Points</span></span>
<span class="line_wrapper">    <span style="color:#696969; ">//</span></span>
<span class="line_wrapper">    p<span style="color:#808030; ">.</span>seed <span style="color:#808030; ">=</span> <span style="color:#008c00; ">200</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">auto</span>    x_vec <span style="color:#808030; ">=</span> gen_lognormal_dist<span style="color:#800080; ">&lt;</span><span style="color:#800000; font-weight:bold; ">double</span><span style="color:#808030; ">,</span> <span style="color:#008c00; ">128</span><span style="color:#800080; ">&gt;</span><span style="color:#808030; ">(</span>item_cnt<span style="color:#808030; ">,</span> p<span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    p<span style="color:#808030; ">.</span>seed <span style="color:#808030; ">=</span> <span style="color:#008c00; ">4356</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">auto</span>                y_vec <span style="color:#808030; ">=</span> gen_lognormal_dist<span style="color:#800080; ">&lt;</span><span style="color:#800000; font-weight:bold; ">double</span><span style="color:#808030; ">,</span> <span style="color:#008c00; ">128</span><span style="color:#800080; ">&gt;</span><span style="color:#808030; ">(</span>item_cnt<span style="color:#808030; ">,</span> p<span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    StlVecType<span style="color:#800080; ">&lt;</span>Point<span style="color:#800080; ">&gt;</span>   points<span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    points<span style="color:#808030; ">.</span>reserve<span style="color:#808030; ">(</span>item_cnt<span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">for</span> <span style="color:#808030; ">(</span><span style="color:#603000; ">size_t</span> i <span style="color:#808030; ">=</span> <span style="color:#008c00; ">0</span><span style="color:#800080; ">;</span> i <span style="color:#808030; ">&lt;</span> item_cnt<span style="color:#800080; ">;</span> <span style="color:#808030; ">+</span><span style="color:#808030; ">+</span>i<span style="color:#808030; ">)</span></span>
<span class="line_wrapper">        points<span style="color:#808030; ">.</span>push_back<span style="color:#808030; ">(</span>Point<span style="color:#808030; ">(</span>x_vec<span style="color:#808030; ">[</span>i<span style="color:#808030; ">]</span><span style="color:#808030; ">,</span> y_vec<span style="color:#808030; ">[</span>i<span style="color:#808030; ">]</span><span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    df<span style="color:#808030; ">.</span>load_column<span style="color:#800080; ">&lt;</span>Point<span style="color:#800080; ">&gt;</span><span style="color:#808030; ">(</span><span style="color:#800000; ">"</span><span style="color:#0000e6; ">point_col</span><span style="color:#800000; ">"</span><span style="color:#808030; ">,</span> <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span><span style="color:#603000; ">move</span><span style="color:#808030; ">(</span>points<span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    KMeansVisitor<span style="color:#800080; ">&lt;</span><span style="color:#008c00; ">5</span><span style="color:#808030; ">,</span> Point<span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">unsigned</span> <span style="color:#800000; font-weight:bold; ">long</span><span style="color:#808030; ">,</span> <span style="color:#008c00; ">128</span><span style="color:#800080; ">&gt;</span> km_visitor2<span style="color:#808030; ">(</span><span style="color:#008c00; ">1000</span><span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">true</span><span style="color:#808030; ">,</span> point_distance<span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    df<span style="color:#808030; ">.</span>single_act_visit<span style="color:#800080; ">&lt;</span>Point<span style="color:#800080; ">&gt;</span><span style="color:#808030; ">(</span><span style="color:#800000; ">"</span><span style="color:#0000e6; ">point_col</span><span style="color:#800000; ">"</span><span style="color:#808030; ">,</span> km_visitor2<span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    <span style="color:#696969; ">// Using the calculated means, separate the given column into clusters</span></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">const</span> <span style="color:#800000; font-weight:bold; ">auto</span>  <span style="color:#808030; ">&amp;</span>clusters2 <span style="color:#808030; ">=</span> km_visitor2<span style="color:#808030; ">.</span>get_clusters<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper">    <span style="color:#800000; font-weight:bold; ">for</span> <span style="color:#808030; ">(</span><span style="color:#800000; font-weight:bold; ">auto</span> iter <span style="color:#800080; ">:</span> clusters2<span style="color:#808030; ">)</span>  <span style="color:#800080; ">{</span></span>
<span class="line_wrapper">        <span style="color:#800000; font-weight:bold; ">for</span> <span style="color:#808030; ">(</span><span style="color:#800000; font-weight:bold; ">auto</span> iter2 <span style="color:#800080; ">:</span> iter<span style="color:#808030; ">)</span></span>
<span class="line_wrapper">            <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span><span style="color:#603000; ">cout</span> <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> iter2<span style="color:#808030; ">.</span>x <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> <span style="color:#800000; ">"</span><span style="color:#0000e6; "> | </span><span style="color:#800000; ">"</span> <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> iter2<span style="color:#808030; ">.</span>y <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> <span style="color:#800000; ">"</span><span style="color:#0000e6; ">, </span><span style="color:#800000; ">"</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">        <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span><span style="color:#603000; ">cout</span> <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> <span style="color:#800000; ">"</span><span style="color:#0f69ff; ">\n</span><span style="color:#0f69ff; ">\n</span><span style="color:#800000; ">"</span> <span style="color:#808030; ">&lt;</span><span style="color:#808030; ">&lt;</span> <span style="color:#666616; ">std</span><span style="color:#800080; ">::</span><span style="color:#603000; ">endl</span><span style="color:#800080; ">;</span></span>
<span class="line_wrapper">    <span style="color:#800080; ">}</span></span>
<span class="line_wrapper"></span>
<span class="line_wrapper"><span style="color:#696969; ">/*</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    found = false;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    for (auto iter : clusters2)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        if (::fabs(iter[0].x - 18.9556) &lt; 0.1 &amp;&amp;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            ::fabs(iter[0].y - 2.17537) &lt; 0.1)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            if (::fabs(iter[6].x - 16.7309) &lt; 0.1 &amp;&amp;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                ::fabs(iter[6].y - 0.872376) &lt; 0.1)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                found = true;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                break;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    assert(found);</span></span>
<span class="line_wrapper"><span style="color:#696969; ">*/</span></span>
<span class="line_wrapper"><span style="color:#696969; ">/*</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    found = false;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    for (auto iter : clusters2)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        if (::fabs(iter[0].x - 0.943977) &lt; 0.1 &amp;&amp;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            ::fabs(iter[0].y - 0.910989) &lt; 0.1)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            if (::fabs(iter[2].x - 0.30509) &lt; 0.1 &amp;&amp;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                ::fabs(iter[2].y - 1.69017) &lt; 0.1)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                found = true;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                break;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    assert(found);</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    found = false;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    for (auto iter : clusters2)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        if (::fabs(iter[0].x - 4.31973) &lt; 0.1 &amp;&amp;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            ::fabs(iter[0].y - 1.24214) &lt; 0.1)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            if (::fabs(iter[3].x - 4.68381) &lt; 0.1 &amp;&amp;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                ::fabs(iter[3].y - 0.453632) &lt; 0.1)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                found = true;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">                break;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    assert(found);</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    found = false;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    for (auto iter : clusters2)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        if (::fabs(iter[0].x - 1.5694) &lt; 0.1 &amp;&amp;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            ::fabs(iter[0].y - 15.3338) &lt; 0.1)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            found = true;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            break;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    assert(found);</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    found = false;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    for (auto iter : clusters2)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        if (::fabs(iter[0].x - 1.29624) &lt; 0.1 &amp;&amp;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            ::fabs(iter[0].y - 4.13919) &lt; 0.1)  {</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            found = true;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">            break;</span></span>
<span class="line_wrapper"><span style="color:#696969; ">        }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    }</span></span>
<span class="line_wrapper"><span style="color:#696969; ">    assert(found);</span></span>
<span class="line_wrapper"><span style="color:#696969; ">*/</span></span>
<span class="line_wrapper"><span style="color:#800080; ">}</span></span>
<span class="line_wrapper"></span></pre>

  <BR><img src="https://github.com/hosseinmoein/DataFrame/blob/master/docs/LionLookingUp.jpg?raw=true" alt="C++ DataFrame"
       width="200" height="200" style="float:right"/>

</body>
</html>

<!--
Local Variables:
mode:HTML
tab-width:4
c-basic-offset:4
End:
-->
